package com.bls.executor.word.noun;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import com.bls.action.BLSAction;
import com.bls.constant.ResultCodeEnum;
import com.bls.exception.MyException;
import com.bls.remote.service.CoreRemoteService;
import com.bls.utils.BandCore;
import lombok.Data;

import java.lang.reflect.InvocationTargetException;
import java.net.URLEncoder;
import java.util.Map;

@Data
public class Chatroom extends Noun {

    CoreRemoteService coreRemoteService = null;
    {
        try {
            coreRemoteService = BLSAction.getBean(CoreRemoteService.class);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    // chatroomID
    private String chatroomId;
    private String bandId;
    private String text;

    public Chatroom(String text, Map<String, Object> scriptEnvironment, String bandId) throws MyException {
        this.text = text;
        this.bandId = bandId;
        init(text, scriptEnvironment);
    }

    private void init(String text, Map<String, Object> scriptEnvironment) throws MyException {
        if(text.startsWith("\"")){
            text = text.substring(1, text.length() - 1);
        }else{
            text = text.trim();
        }
        JSONObject jsonObject = null;
        try {
            jsonObject = coreRemoteService.getChatroomByChatroomName(text, bandId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (((int) jsonObject.get("total")) ==0) {
            throw new MyException(ResultCodeEnum.RUN_ERROR_CHATROOM.getCode(),ResultCodeEnum.RUN_ERROR_CHATROOM.getMsg());
        }
        JSONArray rows = jsonObject.getJSONArray("rows");
        String chatroomID = (String) rows.getJSONObject(0).get("chatroomID");
        this.chatroomId = chatroomID;
    }

    @Override
    public boolean run(String methodName) {
        return super.run(methodName);
    }


    public static void main(String[] args) throws Exception {
        String text ="2019级梁子豪论文消息板";
        String query = "{\"name\":\"%s\"}";
        query = String.format(query,text);
        String encodeQuery = URLEncoder.encode(query, "UTF-8");

//        String httpurl = "https://www.wetoband.com/core//core/v4/chatroom/me?format=json&aid=21419389378723787447&query=%s";
//        String httpurl = "https://www.wetoband.com/core//core/v4/chatroom/me?format=json&aid=21419389378723787447";
//String httpurl = "https://www.wetoband.com/core//core/v4/band/%s/chatroom?format=json&aid=21419389378723787447&query=%s";
        String httpurl = "https://www.wetoband.com/core//core/v4/band/%s/chatroom?format=json&aid=21419389378723787447&gid=%s&query=%s";

// 314507610
        String accessToken = BandCore.getAccessToken();
        String gid = "314539854";
        // 私聊的部署gid写 145  /  35
        httpurl=String.format(httpurl,gid,gid ,encodeQuery);
        System.out.println(httpurl);

        String response = BandCore.doGetWithAccessToken(httpurl, accessToken);
        System.out.println(response);
        JSONObject jsonObject = JSON.parseObject(response);
        if (((int) jsonObject.get("total")) ==0) {
            throw new RuntimeException("查不到该消息板信息："+text);
        }
        JSONArray rows = jsonObject.getJSONArray("rows");
        String chatroomID = (String) rows.getJSONObject(0).get("chatroomID");
        System.out.println("chatroomID = " + chatroomID);
    }

}
